package com.china08.yxyapi.api.manage.sch.platform.resource.yxylectureprepack;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.util.LectureUtil;

/**
 * 学校-备课包管理-待审核--审核通过/不通过
 * 
 * @author LiuWeidong
 *
 */

@CrossOrigin
@RestController
public class YxyLectureSchPrePackWaitAuditPassController extends YxyApiBaseController {
    @Autowired
    private LectureUtil lectureUtil;
    @Autowired
    YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/resource/lectureprepack/waitaudit/pass/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        RespModel model = new RespModel();
        YxyLecturePrePack lecturePrePack = yxyLecturePrePackRepository.findById(reqModel.packId);
        // Assert.isNull(null != lecturePrePack, "没有该备课包！");

        // 通过
        if (reqModel.status == 1) {
            lecturePrePack.setAuditStats(1);
            lecturePrePack.setAuditUserName(getProfile(request, response).getUserNick());
            lecturePrePack.setAuditUserId(getProfile(request, response).getId());
            lecturePrePack.setAuditSchoolId(getProfile(request, response).getSchoolId());
            lecturePrePack.setAuditSchoolName(getProfile(request, response).getSchoolName());
            yxyLecturePrePackRepository.save(lecturePrePack);
            model.setMessage("通过成功");
        }
        // 不通过
        else {
            lecturePrePack.setAuditStats(2);
            lecturePrePack.setAuditComment(reqModel.auditComment);
            lecturePrePack.setAuditUserName(getProfile(request, response).getUserNick());
            lecturePrePack.setAuditUserId(getProfile(request, response).getId());
            lecturePrePack.setAuditSchoolId(getProfile(request, response).getSchoolId());
            lecturePrePack.setAuditSchoolName(getProfile(request, response).getSchoolName());
            yxyLecturePrePackRepository.save(lecturePrePack);
            model.setMessage("不通过成功");
        }
        lectureUtil.saveYt(lecturePrePack.getAreaIdDistrict(), lecturePrePack.getSchoolId(), lecturePrePack.getUserId(),
                lecturePrePack.getUserNick(), lecturePrePack.getSchoolNick(), lecturePrePack.getCreatedDate());

        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        // 通过:1,不通过:0
        public Integer status;
        // 包id
        public String packId;
        // 不通过原因
        public String auditComment;

    }

    protected static class RespModel extends YxyApiRespModel {

    }

}
